<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2008 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/

abstract class URL
{

	public static function load($url='', $full=false)
	{
		$url = DOMAIN_INDEX .'?' .$url;
		if ($full) $url = BASEHREF .$url;
		return $url;
	}

	public static function index($url='', $UseLEO=true, $full=false)
	{
		global $module_name, $MAIN_CFG;
		if (empty($url) || $url[0] == '&') $url = $module_name.$url;
		if (is_object($MAIN_CFG) && $MAIN_CFG->seo->leo && $UseLEO) {
			$end = $MAIN_CFG->seo->leoend;
			$url = str_ireplace('&amp;', '/', $url);
			$url = str_replace('&', '/', $url);
			$url = str_replace('?', '/', $url);
			if (false !== strpos($url, '/file=')) {
				$url = str_replace('/file=', '/', $url);
			}
			if (false !== strpos($url, '#')) {
				$url = str_replace('#', $end.'#', $url);
			} else $url .= $end;
		} else {
			$url = '?name=' .$url;
		}
		if (is_object($MAIN_CFG) && 'lower' == $MAIN_CFG->seo->leocase) $url = strtolower($url);
		if ($full) $url = BASEHREF .$url;
		return $url;
	}

	public static function admin($url='', $full=false)
	{
		global $op, $module_name, $MAIN_CFG;
		if (empty($op) && !empty($module_name)) $op = $module_name;
		if (empty($url)) { $url = $op; }
		if ($url[0] == '&') { $url = DOMAIN_ADMIN .'&op=' .$op .$url; }
		else { $url = DOMAIN_ADMIN .'&op=' .$url; }
		if ($full) $url = BASEHREF .$url;
		return preg_replace('/&(?![a-z#0-9]+;)/is', '&amp;', $url);
	}

	public static function encode($url)
	{
		$url = str_replace('&', '%26', $url);
		$url = str_replace('/', '%2F', $url);
		$url = str_replace('.', '%2E', $url);
		return $url;
	}

	public static function refresh($url='', $time=3)
	{
		global $MAIN_CFG;
		$url = str_ireplace('&amp;', '&', $url);
		if (false === strpos($url, '://') && $MAIN_CFG['server']['path'] != substr($url, 0, strlen($MAIN_CFG['server']['path']))) {
			$url = $MAIN_CFG['server']['path'].$url;
		}
		header('Refresh: ' .intval($time) .'; url='.$url);
	}

	public static function redirect($url='', $redirect=false, $force=0)
	{
		global $SESS, $CPG_SESS, $MAIN_CFG;
		$url = str_ireplace('&amp;', '&', $url);
		if ($redirect) $CPG_SESS['user']['redirect'] = URL::uri();
		$force = intval($force ?: 302);

		$type = 'Location: ';
		if (preg_match('/IIS|Microsoft|WebSTAR|Xitami/', $_SERVER['SERVER_SOFTWARE'])) {
			$type = 'Refresh: 0; URL=';
		}
		if (is_object($SESS)) $SESS->close();
		//header("HTTP/1.1 303 REDIRECT");
		if (false === strpos($url, '://') && $MAIN_CFG['server']['path'] != substr($url, 0, strlen($MAIN_CFG['server']['path']))) {
			$url = BASEHREF .$url;
		}
		header($type .$url, true, $force);
		//header("Status: $force");
		exit;
	}

	public static function uri()
	{
		static $REQUEST_URI;
		if (empty($REQUEST_URI)) {
			if (!empty($_SERVER['REQUEST_URI'])) {
				$REQUEST_URI = $_SERVER['REQUEST_URI'];
			} else {
				$REQUEST_URI = $_SERVER['SCRIPT_NAME'];
				if (!empty($_SERVER['QUERY_STRING'])) {
					$REQUEST_URI .= '?'.$_SERVER['QUERY_STRING'];
				}
			}
			# firefox encodes url by default but others don't
			# Decodes any %## to hex value, + are decoded to a space character.
			$REQUEST_URI = urldecode($REQUEST_URI);
			# encode the url " %22 and <> %3C%3E
			$REQUEST_URI = str_replace('"', '%22', $REQUEST_URI);
			$REQUEST_URI = preg_replace('#([\x3C\x3E])#e', '"%".bin2hex(\'\\1\')', $REQUEST_URI);
			$REQUEST_URI = substr($REQUEST_URI, 0, strlen($REQUEST_URI)-strlen(stristr($REQUEST_URI, '&' .DF_SESSION_NAME)));
		}
		return $REQUEST_URI;
	}

	public static function canonical($raw=false)
	{
		if (empty($_GET['name'])) return '';
		$c = $_GET['name'];
		foreach ($_GET as $key => $val) {
			if ('name' == $key) continue;
			$c .= '&'.$key.'='.$val;
		}
		if ($raw) return $c;
		else return self::index($c);
	}

	public static function query()
	{
		static $q;
		if (empty($q)) {
			$q = substr(self::uri(), strlen(DOMAIN_PATH));
		}
		return $q;
	}

}
